/********************************************************************************/
/*																				*/
/*	Project:	Tianwan NPP FHM CCS												*/
/*	Library:	Twn_Pro_ph.dll													*/
/*	Functions:	Contains functions to plan technological cycle's movements		*/						
/*	Author:		Matteo Tavella													*/
/*	Date:		11/03/2002														*/
/*																				*/
/********************************************************************************/

#define ALONE_TWN_PROPH
#include "Twn_Pro_ph.h"

PHASTAT		phstat;
RETSTAT		stat;
GENIDX		current_mm;
int			PPH_Err;
TCHAR		PPH_LogMsg[128];
TCHAR		PPH_LogPath[128];
BOOL		PPH_LogActive;
BOOL		PPH_MsgBoxActive;


BOOL WINAPI DllMain(HINSTANCE hinstDLL,  // handle to DLL module
					DWORD fdwReason,     // reason for calling function
					LPVOID lpReserved )  // reserved
					{
	LONG	res;
	LPDWORD	type;
	LPBYTE	data;
	LPDWORD size;
	HKEY	hKey;
	TCHAR	szProductType[MY_BUFSIZE];
    DWORD	dwBufLen = MY_BUFSIZE;
	DWORD	active;
    
	size = 0;
	data = 0;
	type = 0;
	
	
	switch(fdwReason){ 
		case DLL_PROCESS_ATTACH:
			RegOpenKeyEx(	HKEY_LOCAL_MACHINE, 
							TEXT("SOFTWARE\\ABB\\Power Generation Portal\\Tianwan\\Twn_Pro_ph"),
							0,
							KEY_QUERY_VALUE,
							&hKey);
			res = RegQueryValueEx(	hKey,
									TEXT("LogPath"),
									NULL,
									NULL,
									(LPBYTE)szProductType,
									&dwBufLen);
			if(res == ERROR_SUCCESS)
				_stprintf_s(PPH_LogPath, (TCHAR*)szProductType);
			
			active = 0;
			dwBufLen = sizeof(active);
			res = RegQueryValueEx(	hKey,
									TEXT("LogActive"),
									NULL,
									NULL,
									(LPBYTE)&active,
									&dwBufLen);
			if(res == ERROR_SUCCESS){
				if((DWORD)active == 0)
					PPH_LogActive = false;
				else
					PPH_LogActive = true;
			}
			
			active = 0;
			dwBufLen = sizeof(active);
			res = RegQueryValueEx(	hKey,
									TEXT("MsgBoxActive"),
									NULL,
									NULL,
									(LPBYTE)&active,
									&dwBufLen);
			if(res == ERROR_SUCCESS){
				if((DWORD)active == 0)
					PPH_MsgBoxActive = false;
				else
					PPH_MsgBoxActive = true;
			}
			break;
		default:
			break;
	}
    return TRUE;  // Successful DLL_PROCESS_ATTACH.
}

/*********************************************************************************************
**********************************************************************************************
**
** Function:		Pro_Ph_Decl
**
** Description:		Manage DECLARE Macro Command
**
** Return:			PHASTAT		-	PH_NORMAL	-	Phase successfully managed.
**								-	PH_XXXXXX	-	Otherwise.
** 
**********************************************************************************************
*********************************************************************************************/
PHASTAT Pro_Ph_Decl( void ){
	GENIDX		ObjtIdx;
	GENIDX		ObjectIdx;
	GENIDX		ObjectObjtIdx;
	GENIDX		SourZoneIdx;
	GENIDX		MacroCmdIdx;
	MAC_CMDNAME	MacroCmdName;
	OBJTCELL	ObjtCell;
	t_cell		S_Virt_Par;
	short int	S_Virt_X, S_Virt_Y;
	short int	D_Virt_X, D_Virt_Y;
	float		S_Phis_X;
	float		S_Phis_Y;
	TCHAR		ObjtName[L_OBJT_NAM];
	TCHAR		ObjectName[L_OBJT_NAM];
	int			ObjtGenCode;
	int			current_phase;
	
	stat = GetDynCurrentPhase(&current_phase);
	stat = GetPhaseMacroName(current_phase, MacroCmdName);
	if(stat == RT_NORMAL){
		//Prelevo l'Idx della macro-op.//
		stat = GetMacroCmdIdx(MacroCmdName, &MacroCmdIdx);
		if(stat != RT_NORMAL)
			return(PH_FAILED);
	}
	else
		return(PH_FAILED);

	stat = GetDynCurrentPhase(&current_phase);
	if(stat != RT_NORMAL){
		_stprintf_s(PPH_LogMsg, _T("Error: cannot retrieve dynamic current phase."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(1));
	}

	/* READ OBJECT TYPE NAME AND IDX TO DECL (FROM PHASE) */
	stat = GetPhaseObjtName(current_phase, ObjtName);
	if(stat == RT_NORMAL){
		stat = GetObjtIdx(ObjtName, &ObjtIdx);
		if (stat != RT_NORMAL){
			_stprintf_s(PPH_LogMsg, _T("Error: DECL command unable to find phase object type Idx from object type name."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(72));
			return (PH_INVOBJT);
		}
	}
	else{
		_stprintf_s(PPH_LogMsg, _T("Error: DECL command unable to retrieve phase object type."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(73));
		return (PH_INVOBJT);
	}

	stat = GetObjtGenericTypeCode(ObjtIdx, &ObjtGenCode);
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: DECL command unable to retrieve phase object type generic code."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(74));
		return (PH_INVOBJT);
	}
	if ((ObjtGenCode != OBJT_N_FA) && (ObjtGenCode != OBJT_N_CLUS)){
		_stprintf_s(PPH_LogMsg, _T("Error: DECL command not available for this object type."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(75));
		return (PH_INVOBJT);
	}

	/* READ OBJECT NAME AND IDX TO DECL (FROM PHASE) */
	stat = GetPhaseObjectName(current_phase, ObjectName);
	if(stat == RT_NORMAL){
		stat = GetObjIdx(ObjectName, &ObjectIdx);
		if ( stat != RT_NORMAL ){
			_stprintf_s(PPH_LogMsg, _T("Error: DECL command unable to find phase object Idx."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(76));
			return (PH_INVOBJ);
		}
	}
	else{
		_stprintf_s(PPH_LogMsg, _T("Error: DECL command unable to find phase object name."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(77));
		return (PH_INVOBJ);
	}

	/* READ SOURCE ZONE AND COORDINATES (FROM PHASE) */
	stat = GetPhaseSourceVirtCoo ( current_phase, &S_Virt_X, &S_Virt_Y );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: DECL command unable to find phase source virtual coordinates."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(78));
		return (PH_INVSOUR);
	}
	stat = GetZoneIdxVirtCoo ( S_Virt_X, S_Virt_Y, &SourZoneIdx );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: DECL command unable to find phase source zone Idx."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(79));
		return (PH_NESOUR);
	}
	stat = GetZoneVirtPar ( S_Virt_X, S_Virt_Y, &SourZoneIdx, &S_Virt_Par );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: DECL command unable to find phase source virtual parameters."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(80));
		return (PH_NESOUR);
	}
	S_Phis_X = S_Virt_Par.Phys_X;
	S_Phis_Y = S_Virt_Par.Phys_Y;
	stat = CheckZonePhyCooForb ( SourZoneIdx, S_Phis_X, S_Phis_Y );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: DECL command unable to find phase source zone forbidden areas."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(81));
		return (PH_INVSOUR);
	}

	/* DESTINATION ZONE COORDINATES MUST BE INEXISTENT */
	stat = GetPhaseDestVirtCoo ( current_phase, &D_Virt_X, &D_Virt_Y );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: DECL command unable to find phase dest. virtual coordinates."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(82));
		return (PH_ERRDEST);
	}
	if ( ( D_Virt_X != 0 ) || ( D_Virt_Y != 0 ) ){
		_stprintf_s(PPH_LogMsg, _T("Error: DECL command dest virtual coordinates must be (0,0)."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(83));
		return (PH_ERRSOUR);
	}

	/* CHECK IF DECL COMMAND IS LEGAL FOR OBJECT TYPE */
	stat = CheckMacroLegalObject ( MacroCmdIdx, ObjtIdx );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: DECL command invalid for object type %d."), ObjtIdx);
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(84));
		return (PH_OBJMAC);
	}
	
	/* CHECK IF SOURCE ZONE IS LEGAL FOR OBJECT TYPE */
	stat = CheckObjtLegalSource ( ObjtIdx, SourZoneIdx );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: DECL command invalid source zone for object type %d."), ObjtIdx);
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(85));
		return (PH_OBJSOUR);
	}
	/*
	stat = CheckZoneVirtPLegalObjt ( &S_Virt_Par, ObjtIdx );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: DECL command invalid virtual parameters for object type %d."), ObjtIdx);
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(86));
		return (PH_OBJVIRT);
	}
	*/

	/* GET OBJECT TYPE OF OBJECT TO DECL */
	stat = GetObjObjtIdx ( ObjectIdx, &ObjectObjtIdx );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: DECL command unable to retrieve object type for phase object."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(87));
		return (PH_INVOBJ);
	}

	/* CHECK IF NOT MATCH WITH OBJECT TYPE IN PHASE */
	if ( ObjectObjtIdx == ObjtIdx ){
		_stprintf_s(PPH_LogMsg, _T("Warning: tryng to declare an object with its same type."));
		PPH_LogFileWrite(_T("WRN"), PPH_LogMsg, PPH_ERR_CODE(88));
		return (PH_MISMOBJ);
	}

	/* GET ACTUAL CELL OBJECT SITUATION */
	stat = GetCellSituation ( S_Virt_X, S_Virt_Y, &ObjtCell );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: DECL command unable to retrieve actual cell situation."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(89));
		return (PH_OBJVIRT);
	}

	/* CHANGE OBJECT IN CELL */
	//Da Mov_Volgodonsk.dll serve per cambiare un oggetto dentro una cella.//
	stat = CngObjtInCellSituation ( &ObjtCell, ObjectIdx, ObjtIdx );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: DECL command unable to change object in cell situation."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(90));
		return (PH_OBJVIRT);
	}

	/* UPDATE CELL OBJECT SITUATION */
	stat = UpdateCellSituation ( S_Virt_X, S_Virt_Y, &ObjtCell );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: DECL command unable to update cell situation."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(91));
		return (PH_OBJVIRT);
	}

	/* CHANGE OBJECT TYPE FOR OBJECT */
	stat = PutObjObjtIdx ( ObjectIdx, ObjtIdx );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: DECL command unable to change object type for object."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(92));
		return (PH_INVOBJ);
	}
	return(PH_NORMAL);
}

/*********************************************************************************************
**********************************************************************************************
**
** Function:		Pro_Ph_Insr
**
** Description:		Manage INSERT Macro Command
**
** Return:			PHASTAT		-	PH_NORMAL	-	Phase successfully managed.
**								-	PH_XXXXXX	-	Otherwise.
** 
**********************************************************************************************
*********************************************************************************************/
PHASTAT Pro_Ph_Insr( void ){
	RETCOND		retsta;
	t_cell		S_Virt_Par;
	OBJTCELL	ObjtCell;
	OBJTNAME	ObjtName;
	OBJNAME		ObjectName;
	ZONENAME	zoneName;
	GENIDX		ObjtIdx;
	GENIDX		ObjectIdx;
	GENIDX		realSourceZoneIdx;
	GENIDX		SourZoneIdx;
	GENIDX		tempIdx;
	GENIDX		HercaZone1Idx;
	GENIDX		HercaZone2Idx;
	GENIDX		IOZoneIdx;
	GENIDX		FADSZoneIdx;
	GENIDX		MainZoneIdx;
	GENIDX		cellType;
	short int	S_Virt_X, S_Virt_Y;
	short int	D_Virt_X, D_Virt_Y;
	float		S_Phis_X;
	float		S_Phis_Y;
	t_obj		obj_e;
	int			recnbr=0;
	short		idxnbr=0, counter=0,irec, err_ctr=0;
	short		i,totrec=0;
	static short found=0;
	int			ObjtGenCode;
	FILEIDX		fileObje;
	FILEIDX		fileOpms;
	fileObje.num = F_OBJE;
	fileOpms.num = F_OPMS;
	int			current_phase;
	MAC_CMDNAME	MacroCmdName;
	GENIDX		MacroCmdIdx;

	//Prelevo dli Idx delle zone.//
	GetZoneIdx(ZONE_MAIN, &MainZoneIdx);
	GetZoneIdx(ZONE_IO, &IOZoneIdx);
	GetZoneIdx(ZONE_HECA_1, &HercaZone1Idx);
	GetZoneIdx(ZONE_HECA_2, &HercaZone2Idx);
	GetZoneIdx(ZONE_FFDS, &FADSZoneIdx);

	stat = GetDynCurrentPhase(&current_phase);
	stat = GetPhaseMacroName (current_phase, MacroCmdName);
	if(stat == RT_NORMAL){
		//Prelevo l'Idx della macro-op.//
		stat = GetMacroCmdIdx ( MacroCmdName, &MacroCmdIdx );
		if(stat != RT_NORMAL)
			return(PH_FAILED);
	}
	else
		return(PH_FAILED);

	stat = GetDynCurrentPhase ( &current_phase );
	if(stat != RT_NORMAL){
		_stprintf_s(PPH_LogMsg, _T("Error: cannot retrieve dynamic current phase."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(1));
	}
	/* READ OBJECT TYPE NAME AND IDX TO INSR (FROM PHASE) */
	stat = GetPhaseObjtName(current_phase, ObjtName);
	if(stat == RT_NORMAL){
		stat = GetObjtIdx ( ObjtName, &ObjtIdx );
		if ( stat != RT_NORMAL ){
			_stprintf_s(PPH_LogMsg, _T("Error: INSR command unable to retrieve phase object type name."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(93));
			return (PH_INVOBJT);
		}
	}
	else{
		_stprintf_s(PPH_LogMsg, _T("Error: INSR command unable to retrieve phase object type Idx."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(94));
		return (PH_INVOBJT);
	}

	/* READ OBJECT NAME TO INSR (FROM PHASE) */
	stat = GetObjtGenericTypeCode  ( ObjtIdx, &ObjtGenCode );
	if ( ( stat == RT_NORMAL ) && ( ObjtGenCode < N_MAX_NAMED_OBJ ) ){
		stat = GetPhaseObjectName ( current_phase, ObjectName );
		if ( stat != RT_NORMAL ){
			_stprintf_s(PPH_LogMsg, _T("Error: INSR command unable to retrieve phase object name."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(95));
			return (PH_INVOBJ);
		}
	}
	else
		ObjectIdx = 0;

	/* READ SOURCE ZONE AND COORDINATES (FROM PHASE) */
	stat = GetPhaseSourceVirtCoo ( current_phase, &S_Virt_X, &S_Virt_Y );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: INSR command unable to retrieve phase source virtual coordinates."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(96));
		return (PH_INVSOUR);
	}

	/* CHECK IF OBJECT TYPE IS OF AN NAMED OBJECT TYPE */
	stat = GetObjtGenericTypeCode (ObjtIdx, &ObjtGenCode);
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: INSR command unable to retrieve object Idx from phase object."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(97));
		return (PH_INVOBJT);
	}

	/*********************************************************************/
	/* This part of code regards the updating of file 366.dat, the       */
	/* instruction are partially getting from BOBJE.C program.       cdm */
	/*********************************************************************/

	if ( ObjtGenCode < N_MAX_NAMED_OBJ ){
		memset (&obj_e,0,sizeof(obj_e));                                         
		/* Verify if object name already exists in F.OBJE */
		totrec++;                                                                
		found = 0;                                                      
		irec=1;                                                                 
		for (i=0; i< N_MAX_OBJ; i++){                                                                       
			retsta = FileRead(&fileObje, irec,1, &obj_e, sizeof(obj_e));                      
			if (retsta != R_NORMAL){                                                
				_stprintf_s(PPH_LogMsg, _T("Error: INSR command unable to read F.OBJE DAFS file."));
				PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(98));
				return (PH_FAILED);
			}
			if (_tcscmp(obj_e.name, ObjectName) == 0) {
				found++;
				break;
			}
			else{
				if (!_tcscmp(obj_e.name, _T(""))) {
					recnbr = irec;
					break;
				}
			}
			irec++;
		}
		ObjectIdx = recnbr;
	}else
		found = 0;

	/* if object name already exist in file send a crt message  */
	if(found){
		_stprintf_s(PPH_LogMsg, _T("Error: INSR command object %s already present in the system."), ObjectName );
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(135));
		return (PH_OBJEXIST);
	}
	
	stat = GetZoneIdxVirtCoo ( S_Virt_X, S_Virt_Y, &SourZoneIdx );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: INSR command unable to retrieve phase source zone Idx."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(99));
		return (PH_NESOUR);
	}

	stat = GetZoneVirtPar ( S_Virt_X, S_Virt_Y, &SourZoneIdx, &S_Virt_Par );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: INSR command unable to retrieve phase source virtual parameters."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(100));
		return (PH_NESOUR);
	}

	//Controllo se la zona � riconducibile a Z2...//
	stat = GetZoneName(SourZoneIdx, zoneName);
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: INSR command unable to retrieve phase source zone name."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(136));
		return (PH_NESOUR);
	}

	stat = GetSuperZoneFromName(zoneName, &realSourceZoneIdx);
	if (stat == RT_NORMAL){
		tempIdx = SourZoneIdx;
		SourZoneIdx = realSourceZoneIdx;
		realSourceZoneIdx = tempIdx;
	}
	else if(stat == RT_INVZONE){
		_stprintf_s(PPH_LogMsg, _T("Error: INSR command unable to retrieve super zone Idx."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(137));
		return (PH_NESOUR);
	}
	else{
		realSourceZoneIdx = SourZoneIdx;
	}

	S_Phis_X = S_Virt_Par.Phys_X;
	S_Phis_Y = S_Virt_Par.Phys_Y;

	stat = CheckZonePhyCooForb ( realSourceZoneIdx, S_Phis_X, S_Phis_Y );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: INSR command unable to retrieve source zone forbidden areas."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(101));
		return (PH_INVSOUR);
	}

	/* DESTINATION ZONE COORDINATES MUST BE INEXISTENT */
	stat = GetPhaseDestVirtCoo ( current_phase, &D_Virt_X, &D_Virt_Y );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: INSR command unable to retrieve phase dest. virtual coordinates."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(102));
		return (PH_ERRDEST);
	}
	if ( ( D_Virt_X != 0 ) || ( D_Virt_Y != 0 ) ){
		_stprintf_s(PPH_LogMsg, _T("Error: INSR command dest. virtual coordinates must be (0,0)."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(103));
		return (PH_ERRSOUR);
	}

	/* CHECK IF INSR COMMAND IS LEGAL FOR OBJECT TYPE */
	//Funzione di Uti_Volgodonsk che legge dal file F.MACR.//
	stat = CheckMacroLegalObject ( MacroCmdIdx, ObjtIdx );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: INSR command unable to retrieve macro op. legal object types."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(104));
		return (PH_OBJMAC);
	}

	/* CHECK IF SOURCE ZONE IS LEGAL FOR OBJECT TYPE */
	//Se la zona � un'HECA zone...//
	if ((realSourceZoneIdx == HercaZone1Idx) || (realSourceZoneIdx == HercaZone2Idx)){
		if((ObjtGenCode != OBJT_N_FA) && (ObjtGenCode != OBJT_N_HECA) && (ObjtGenCode != OBJT_N_CLUS) && (ObjtGenCode != OBJT_N_COVER)){
			_stprintf_s(PPH_LogMsg, _T("Error: INSR command object type isn't allowed in HECA zone."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(105));
			return (PH_OBJSOUR);
		}
	}
	else{
		stat = CheckObjtLegalSource ( ObjtIdx, SourZoneIdx ); 
		if ( stat != RT_NORMAL ){
			_stprintf_s(PPH_LogMsg, _T("Error: INSR command source is illegal for object type."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(106));
			return (PH_OBJSOUR);
		}
	}
	/*
	stat = CheckZoneVirtPLegalObjt ( &S_Virt_Par, ObjtIdx );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: INSR command illegal object for virtaul parameters."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(107));
		return (PH_OBJVIRT);
	}
	*/
	/* GET ACTUAL CELL OBJECT SITUATION */
	stat = GetCellSituation ( S_Virt_X, S_Virt_Y, &ObjtCell );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: INSR command unable to retrieve actual cell situation."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(108));
		return (PH_OBJVIRT);
	}

	stat = GetCellType(S_Virt_X, S_Virt_Y, &cellType);
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: INSR command unable to retrieve actual cell type."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(108));
		return (PH_OBJVIRT);
	}

	//Controllo se posso inserire l'oggetto nella cella.//
	if(ObjtGenCode == OBJT_N_CLUS){
		if((cellType == CELL_FA) && ((ObjtCell.o_id[OBJT_N_FA] == 0) && (ObjtCell.obj[OBJT_N_CLUS_FRM] == 0))){
			_stprintf_s(PPH_LogMsg, _T("Error: INSR command, can't insert cluster in cell without cassette or cluster frame."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(141));
			return (PH_EMPTYDST);
		}
		else if((cellType == CELL_FA) && (ObjtCell.obj[OBJT_N_CLUS] != 0)){
			_stprintf_s(PPH_LogMsg, _T("Error: INSR command, can't insert cluster,  a cluster is present in cell."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(141));
			return (PH_EMPTYDST);
		}
		else if((cellType == CELL_COVER_PLUG) && (ObjtCell.obj[OBJT_N_CLUS] != 0)){
			_stprintf_s(PPH_LogMsg, _T("Error: INSR command, can't insert cluster,  a cluster is present in cell."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(141));
			return (PH_EMPTYDST);
		}
		else if((cellType == CELL_FFDS_CASK) && ((ObjtCell.obj[OBJT_N_CLUS] != 0) || (ObjtCell.obj[OBJT_N_FFDS_COV] != 0) || (ObjtCell.obj[OBJT_N_FA] == 0))){
			_stprintf_s(PPH_LogMsg, _T("Error: INSR command, can't insert cluster,  a cluster is present in cell."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(141));
			return (PH_EMPTYDST);
		}
		else if((cellType == CELL_HECA) && ((ObjtCell.obj[OBJT_N_CLUS] != 0) || /*(ObjtCell.obj[OBJT_N_COVER] != 0) || */ (ObjtCell.obj[OBJT_N_FA] == 0)  || (ObjtCell.obj[OBJT_N_HECA] == 0))){
			_stprintf_s(PPH_LogMsg, _T("Error: INSR command, can't insert cluster."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(141));
			return (PH_EMPTYDST);
		}
		else if((cellType == CELL_COVER_PLUG_EX) || (cellType == CELL_CORR_VIRT)){
			_stprintf_s(PPH_LogMsg, _T("Error: INSR command, illegal cell to insert cluster"));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(145));
			return (PH_INVDEST);
		}
	}

	if(ObjtGenCode == OBJT_N_FA){
		if(cellType == CELL_HECA){
			if(ObjtCell.o_id[OBJT_N_HECA] == 0){
				_stprintf_s(PPH_LogMsg, _T("Error: INSR command, HECA not present in cell."));
				PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(144));
				return (PH_EMPTYSRC);
			}
		}
		else if(cellType == CELL_FFDS_CASK){
			if(ObjtCell.o_id[OBJT_N_FA] != 0){
				_stprintf_s(PPH_LogMsg, _T("Error: INSR command, FFDS cask is full."));
				PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(144));
				return (PH_FULLDST);
			}
		}
		else if(cellType == CELL_FA){
			if((ObjtCell.obj[OBJT_N_FA] != 0) || (ObjtCell.obj[OBJT_N_CLUS_CAS] != 0) || (ObjtCell.obj[OBJT_N_CLUS_FRM] != 0)){
				_stprintf_s(PPH_LogMsg, _T("Error: INSR command, cassette already present in cell."));
				PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(145));
				return (PH_FULLDST);
			}
		}
		else if((cellType != CELL_FA) && (cellType != CELL_FFDS_CASK) && (cellType != CELL_HECA)){
			_stprintf_s(PPH_LogMsg, _T("Error: INSR command, illegal cell to insert FA"));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(145));
			return (PH_INVDEST);
		}
	}

	if(ObjtGenCode == OBJT_N_HECA){
		if(cellType == CELL_HECA){
			if(ObjtCell.o_id[OBJT_N_HECA] != 0){
				_stprintf_s(PPH_LogMsg, _T("Error: INSR command, HECA already present in cell."));
				PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(148));
				return (PH_FULLDST);
			}
		}
		else{
			_stprintf_s(PPH_LogMsg, _T("Error: INSR command, invalid cell type to insert HECA."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(148));
			return (PH_FULLDST); 
		}
	}
	
	
	if(ObjtGenCode == OBJT_N_COVER){
		if((cellType != CELL_HECA) && (cellType != CELL_COVER_PLUG) && (cellType != CELL_COVER_PLUG_EX)){
			_stprintf_s(PPH_LogMsg, _T("Error: INSR command, can't insert HECA plug in this zone."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(149));
			return (PH_INVDEST);
		}
		else{
			if(((cellType == CELL_HECA) && ((ObjtCell.o_id[OBJT_N_HECA] == 0) || (ObjtCell.o_id[OBJT_N_COVER] != 0))) || (((cellType == CELL_COVER_PLUG) || (cellType == CELL_COVER_PLUG_EX)) && ((ObjtCell.o_id[OBJT_N_COVER] != 0) && (ObjtCell.o_id[OBJT_N_FFDS_COV] != 0)))){
				_stprintf_s(PPH_LogMsg, _T("Error: INSR command, HECA plug already present in cell, or HECA not present in cell."));
				PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(150));
				return (PH_EMPTYSRC);
			}
		}
	}

	if(ObjtGenCode == OBJT_N_CLUS_CAS){
		if(IsClusCasCell(S_Virt_X, S_Virt_Y) != RT_NORMAL){
			_stprintf_s(PPH_LogMsg, _T("Error: INSR command cluster casing for load wrong home position."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(151));
			return (PH_INVDEST);
		}
		if(ObjtCell.obj[OBJT_N_CLUS_CAS] != 0){
			_stprintf_s(PPH_LogMsg, _T("Error: INSR command cluster casing already present in home position."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(156));
			return (PH_FULLDST);
		}
	}

	if(ObjtGenCode == OBJT_N_CLUS_FRM){
		if((cellType == CELL_FA) && ((ObjtCell.obj[OBJT_N_CLUS_FRM] != 0) || (ObjtCell.obj[OBJT_N_FA] != 0))){
			_stprintf_s(PPH_LogMsg, _T("Error: INSR command cluster frame already present in this cell."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(154));
			return (PH_INVDEST);
		}
		else if(cellType != CELL_FA){
			_stprintf_s(PPH_LogMsg, _T("Error: INSR command illegal cell type to insert cluster frame."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(154));
			return (PH_FULLDST);
		}
	}

	if(ObjtGenCode == OBJT_N_FFDS_COV){
		if((cellType != CELL_FFDS_CASK) && (cellType != CELL_COVER_PLUG_EX) && (cellType != CELL_COVER_PLUG)){
			_stprintf_s(PPH_LogMsg, _T("Error: INSR command FADS cover wrong position."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(156));
			return (PH_INVDEST);
		}
		else if(ObjtCell.obj[OBJT_N_FFDS_COV] != 0){
			_stprintf_s(PPH_LogMsg, _T("Error: INSR command FADS cover already present in this position."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(157));
			return (PH_FULLDST);
		}
	}
	

	/* PUT  OBJECT IN  CELL   SITUATION */
	stat = InsObjectInCellSituation ( &ObjtCell, ObjectIdx, ObjtIdx );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: INSR command unable to insert object in cell situation."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(109));
		return (PH_OBJVIRT);
	}

	/* UPDATE CELL OBJECT SITUATION */
	stat = UpdateCellSituation ( S_Virt_X, S_Virt_Y, &ObjtCell );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: INSR command unable to update cell situation."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(110));
		return (PH_OBJVIRT);
	}

	/* Update information in 366.dat file record (F_OBJE)     */
	if ( ObjtGenCode < N_MAX_NAMED_OBJ ){
		retsta = FileRead(&fileObje, recnbr,1, &obj_e, sizeof(obj_e));
		if (retsta != R_NORMAL){
			_stprintf_s(PPH_LogMsg, _T("Error: INSR command unable to read F.OBJE DAFS file."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(111));
			return (PH_FAILED);
		}
		obj_e.obj_type = ObjtIdx;
		obj_e.idx = recnbr;
		memcpy (obj_e.name, ObjectName, L_OBJ_NAME);
		obj_e.obj_pos.x = S_Virt_X;
		obj_e.obj_pos.y = S_Virt_Y;
		//In posizione z metto il peso dell'oggetto prelevandolo dal suo tipo di riferimento.//
		stat = GetObjtWeight(ObjtIdx, &obj_e.obj_pos.z);
		if(stat != RT_NORMAL){
			_stprintf_s(PPH_LogMsg, _T("Error: INSR command unable to retrieve object type weight."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(158));
			return (PH_FAILED);
		}
		retsta = FileWrite(&fileObje, recnbr ,1, &obj_e, sizeof(obj_e));
		if (retsta != R_NORMAL){
			_stprintf_s(PPH_LogMsg, _T("Error: INSR command unable to write on F.OBJE DAFS file."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(112));
			return (PH_FAILED);
		}
	}    
	return(PH_NORMAL);
}

/*********************************************************************************************
**********************************************************************************************
**
** Function:		Pro_Ph_Remo
**
** Description:		Manage REMO Macro Command
**
** Return:			PHASTAT		-	PH_NORMAL	-	Phase successfully managed.
**								-	PH_XXXXXX	-	Otherwise.
** 
**********************************************************************************************
*********************************************************************************************/
PHASTAT Pro_Ph_Remo( void ){
	RETCOND			retsta;
	FILEIDX			fileObje;
	OBJTCELL		ObjtCell;
	OBJTNAME		ObjtName;
	OBJNAME			ObjectName;
	MAC_CMDNAME		MacroCmdName;
	GENIDX			ObjtIdx;
	GENIDX			ObjectIdx;
	GENIDX			SourZoneIdx;
	GENIDX			HercaZone1Idx;
	GENIDX			HercaZone2Idx;
	GENIDX			MacroCmdIdx;
	t_obj			obj_e;
	t_cell			S_Virt_Par;
	short int		S_Virt_X, S_Virt_Y;
	short int		D_Virt_X, D_Virt_Y;
	float			S_Phis_X;
	float			S_Phis_Y;
	static short	found;
	short int		irec, i;
	int				ObjtGenCode;
	int				current_phase;
	
	fileObje.num = F_OBJE;
	found = 0;

	stat = GetDynCurrentPhase(&current_phase);
	if(stat != RT_NORMAL){
		_stprintf_s(PPH_LogMsg, _T("Error: cannot retrieve dynamic current phase."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(1));
	}

	stat = GetPhaseMacroName (current_phase, MacroCmdName);
	if(stat == RT_NORMAL){
		//Prelevo l'Idx della macro-op.//
		stat = GetMacroCmdIdx ( MacroCmdName, &MacroCmdIdx );
		if(stat != RT_NORMAL)
			return(PH_FAILED);
	}
	else
		return(PH_FAILED);

	GetZoneIdx(ZONE_HECA_1, &HercaZone1Idx);
	GetZoneIdx(ZONE_HECA_2, &HercaZone2Idx);

	/* READ OBJECT TYPE NAME AND IDX TO REMO (FROM PHASE) */
	stat = GetPhaseObjtName ( current_phase, ObjtName );
	if ( stat == RT_NORMAL ){
		stat = GetObjtIdx ( ObjtName, &ObjtIdx );
		if ( stat != RT_NORMAL ){
			_stprintf_s(PPH_LogMsg, _T("Error: REMO command unable to retrieve phase object type Idx."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(113));
			return (PH_INVOBJT);
		}
	}
	else{
		_stprintf_s(PPH_LogMsg, _T("Error: REMO command unable to retrieve phase object type name."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(114));
		return (PH_INVOBJT);
	}

	/* READ OBJECT NAME AND IDX TO REMO (FROM PHASE) */
	stat = GetObjtGenericTypeCode  ( ObjtIdx, &ObjtGenCode );
	if ( ( stat == RT_NORMAL ) && ( ObjtGenCode < N_MAX_NAMED_OBJ ) ){
		stat = GetPhaseObjectName ( current_phase, ObjectName );
		if ( stat == RT_NORMAL ){
			stat = GetObjIdx ( ObjectName, &ObjectIdx );
			/****************************************************************************
			M.C. 14/7/99 Eliminato controllo nome oggetto 
			if ( stat != RT_NORMAL ){
				return (PH_INVOBJ);
			}
			****************************************************************************/
		}
		else{
			_stprintf_s(PPH_LogMsg, _T("Error: REMO command unable to retrieve phase object name."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(115));
			return (PH_INVOBJ);
		}
	}
	else
		ObjectIdx = 0;

	/* READ SOURCE ZONE AND COORDINATES (FROM PHASE) */
	stat = GetPhaseSourceVirtCoo ( current_phase, &S_Virt_X, &S_Virt_Y );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: REMO command unable to retrieve phase source virtual coordinates."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(116));
		return (PH_INVSOUR);
	}
	stat = GetZoneIdxVirtCoo ( S_Virt_X, S_Virt_Y, &SourZoneIdx );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: REMO command unable to retrieve phase source zone Idx."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(117));
		return (PH_NESOUR);
	}
	stat = GetZoneVirtPar ( S_Virt_X, S_Virt_Y, &SourZoneIdx, &S_Virt_Par );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: REMO command unable to retrieve phase source zone virtual parameters."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(118));
		return (PH_NESOUR);
	}
	S_Phis_X = S_Virt_Par.Phys_X;
	S_Phis_Y = S_Virt_Par.Phys_Y;
	stat = CheckZonePhyCooForb ( SourZoneIdx, S_Phis_X, S_Phis_Y );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: REMO command unable to retrieve phase source zone forbidden areas."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(119));
		return (PH_INVSOUR);
	}

	/* DESTINATION ZONE COORDINATES MUST BE INEXISTENT */
	stat = GetPhaseDestVirtCoo ( current_phase, &D_Virt_X, &D_Virt_Y );
	if ( stat != RT_NORMAL){
		_stprintf_s(PPH_LogMsg, _T("Error: REMO command unable to retrieve phase dest. virtual coordinates."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(120));
		return (PH_ERRDEST);
	}
	if ( ( D_Virt_X != 0 ) || ( D_Virt_Y != 0 ) ){
		_stprintf_s(PPH_LogMsg, _T("Error: REMO command dest. virtual coordinates must be (0,0)."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(121));
		return (PH_ERRSOUR);
	}

	/* CHECK IF REMO COMMAND IS LEGAL FOR OBJECT TYPE */
	stat = CheckMacroLegalObject ( MacroCmdIdx, ObjtIdx );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: REMO command unable to retrieve macro op. legal object types."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(122));
		return (PH_OBJMAC);
	}

	/*
	stat = CheckZoneVirtPLegalObjt ( &S_Virt_Par, ObjtIdx );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: REMO command unable to retrieve virtual parameters legal object types."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(125));
		return (PH_OBJVIRT);
	}
	*/

	/* GET ACTUAL CELL OBJECT SITUATION */
	stat = GetCellSituation ( S_Virt_X, S_Virt_Y, &ObjtCell );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: REMO command unable to retrieve actual cell situation."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(126));
		return (PH_OBJVIRT);
	}

	//Rimuove l'oggetto dalla cella.//
	stat = GetObjectFromCellSituation ( &ObjtCell, ObjectIdx, ObjtIdx );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: REMO command unable to retrieve object from cell situation."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(127));
		return (PH_OBJVIRT);
	}

	/* UPDATE CELL OBJECT SITUATION */
	stat = UpdateCellSituation ( S_Virt_X, S_Virt_Y, &ObjtCell );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: REMO command unable to update cell situation."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(128));
		return (PH_OBJVIRT);
	}

	/************************************************************************/
	/*  Delete the record relative to the object, from file F_OBJE (366.dat)*/
	/************************************************************************/
	/* Check if object is configured and read the index */
	/* CHECK IF OBJECT TYPE IS OF AN NAMED OBJECT TYPE */
  
	stat = GetObjtGenericTypeCode  ( ObjtIdx, &ObjtGenCode );
	if ( stat != RT_NORMAL ){
		_stprintf_s(PPH_LogMsg, _T("Error: REMO command unable to retrieve generic type code for phase object."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(129));
		return (PH_NORMAL);
	}
	if ( ObjtGenCode < N_MAX_NAMED_OBJ ){
		found = 0;
		irec=1;
		for (i=0; i< N_MAX_OBJ; i++){
			retsta = FileRead(&fileObje, irec,1, &obj_e, sizeof(obj_e));
			if (retsta != R_NORMAL){
				_stprintf_s(PPH_LogMsg, _T("Error: REMO command unable to read F.OBJE DAFS file."));
				PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(130));
				return (PH_NORMAL);
			}
			if (_tcscmp(obj_e.name, ObjectName) == 0) {
				found++;
				break;
			}
			irec++;
		}
		/* if record to delete not found send the message */
		if (found == 0){
			_stprintf_s(PPH_LogMsg, _T("Error: REMO command unable to retrieve object from F.OBJE DAFS file."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(131));
			return (PH_NORMAL);
		}
		else{
			retsta =  Object_Dele (&irec); 
			if (retsta != R_NORMAL){
				_stprintf_s(PPH_LogMsg, _T("Error: REMO command unable to delete object from F.OBJE DAFS file."));
				PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(132));
				return (PH_NORMAL);
			}
			irec = 0;
		}
	}
	return(PH_NORMAL);
}

/*********************************************************************************************
**********************************************************************************************
**
** Function:		Object_Dele
**
** Description:		Manage REMO Macro Command
**
** Input:			idxnbr		-	Number of the record to delete from F.OBJE DAFS file
**
** Return:			PHASTAT		-	PH_NORMAL	-	Phase successfully managed.
**								-	PH_XXXXXX	-	Otherwise.
** 
**********************************************************************************************
*********************************************************************************************/
RETCOND Object_Dele(short *idxnbr){
	RETCOND		retsta;
	t_obj		obj_e;
	FILEIDX		fileObje;

	fileObje.num = F_OBJE;

	retsta = FileRead ( &fileObje, *idxnbr, 1, &obj_e, sizeof(obj_e));
	if (retsta != R_NORMAL){
		_stprintf_s(PPH_LogMsg, _T("Error: Object_Dele unable to read from F.OBJE DAFS file."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(133));
		return (R_FAILED);
	}
	else{
		memset (&obj_e,0 ,sizeof(obj_e));
		retsta = FileWrite ( &fileObje, *idxnbr, 1, &obj_e, sizeof(obj_e));
		if (retsta != R_NORMAL){
			_stprintf_s(PPH_LogMsg, _T("Error: Object_Dele unable to write on F.OBJE DAFS file."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(134));
			return (R_FAILED);
		}
	} 
	return (R_NORMAL);
}

/*********************************************************************************************
**********************************************************************************************
**
** Function:		PPH_ActivateLog
**
** Description:		Activate the Log file.
**
** Return:			int				- 0 for success.
**									- -1 otherwise.
** 
**********************************************************************************************
*********************************************************************************************/
int PPH_ActivateLog(void){
	PPH_LogActive = true;
	return 0;
}

/*********************************************************************************************
**********************************************************************************************
**
** Function:		PPH_DeactivateLog
**
** Description:		Deactivate the Log file.
**
** Return:			int				- 0 for success.
**									- -1 otherwise.
** 
**********************************************************************************************
*********************************************************************************************/
int PPH_DeactivateLog(void){
	PPH_LogActive = false;
	return 0;
}

/*********************************************************************************************
**********************************************************************************************
**
** Function:		PPH_ActivateMsgBox
**
** Description:		Activate the message box error messages.
**
** Return:			int				- 0 for success.
**									- -1 otherwise.
** 
**********************************************************************************************
*********************************************************************************************/
int PPH_ActivateMsgBox(void){
	PPH_MsgBoxActive = true;
	return 0;
}

/*********************************************************************************************
**********************************************************************************************
**
** Function:		PPH_DeactivateMsgBox
**
** Description:		Deactivate the message box error messages.
**
** Return:			int				- 0 for success.
**									- -1 otherwise.
** 
**********************************************************************************************
*********************************************************************************************/
int PPH_DeactivateMsgBox(void){
	PPH_MsgBoxActive = false;
	return 0;
}
/*********************************************************************************************
**********************************************************************************************
**
** Function:		PPH_SetLogPath
**
** Description:		Cahge the Log file path.
**
** Input:			path			- New Log file path.
** Return:			int				- 0 for success.
**									- -1 otherwise.
** 
**********************************************************************************************
*********************************************************************************************/
int PPH_SetLogPath(TCHAR *path){
	_tcscpy_s(PPH_LogPath, path);
	return 0;
}

/*********************************************************************************************
**********************************************************************************************
**
** Function:		PPH_LogFileWrite
**
** Description:		Print a message in the application log file,
**
** Input:			cmd				- _T("ERR") - error, _T("WRN") - warning, _T("OK ") - success.
**					Comment			- Message description.
**
** Return:			int				- 1 for success.
**									- -1 otherwise.
** 
**********************************************************************************************
*********************************************************************************************/
int PPH_LogFileWrite(TCHAR cmd[4], TCHAR *Comment, int errNum){
	FILE		*LogFile;
	TCHAR		newLine = '\n';
	SYSTEMTIME	timeStamp;
	TCHAR		tempStr[256];
	TCHAR		timeStr[100];
	
	if(PPH_LogActive){
		
		errno_t err;
		err = _tfopen_s(&LogFile,PPH_LogPath, _T("a"));
		if (err != 0)
			return -1;

		//LogFile = _tfopen(PPH_LogPath, _T("a"));
		//if(LogFile == NULL)
		//	return -1;
		
		GetSystemTime(&timeStamp);
		_stprintf_s(timeStr, _T("[hh%2d  mm%2d  ss%2d,%3d (GMT)] [%2d-%2d-%4d]"), timeStamp.wHour, timeStamp.wMinute, timeStamp.wSecond, timeStamp.wMilliseconds, timeStamp.wDay, timeStamp.wMonth, timeStamp.wYear);
		if(_tcscmp(cmd, _T("ERR")) == 0)
			_ftprintf(LogFile,_T("%s   %s  %s Err.N� %d.%c"), timeStr, cmd, Comment, errNum,  newLine);
		else
			_ftprintf(LogFile,_T("%s   %s  %s%c"), timeStr, cmd, Comment, newLine);
		if(fclose(LogFile)!=0)
			return -1;
	}
		
	if((PPH_MsgBoxActive) && (_tcscmp(cmd, _T("ERR")) == 0)){
		_stprintf_s(tempStr, _T("Error n.%d, %s"), errNum, Comment);
		MessageBox(NULL, tempStr, _T("Error"), MB_OK | MB_ICONERROR);
	}

	return 0;
}

/*********************************************************************************************
**********************************************************************************************
**
** Function:		ChangeMast
**
** Description:		Change the current mast.
**
** Input:			currMast			- current mast in use.
**					newMast				- new mast in use.
**
** Return:			RETSTAT			- PH_NORMAL for success.
**									- PH_FAILED otherwise.
** 
**********************************************************************************************
*********************************************************************************************/
PHASTAT ChangeMast(GENIDX currMast, GENIDX newMast){
	RETSTAT		retSt;
	RETCOND		retCnd;
	GENIDX		dynMast;
	float		dynx, dyny;
	float		dynX, dynY;
	float		falv_x, falv_y;
	OBJTMM		mastSituation;
	GENIDX		zoneIdx;
	GENIDX		sfpIdx;

	dynx = 0.0;
	dyny = 0.0;

	GetFALVOffset(&falv_x, &falv_y);

	retSt = GetZoneIdx(ZONE_MAIN, &sfpIdx);
	if(retSt != RT_NORMAL){
		_stprintf_s(PPH_LogMsg, _T("Error: unable to retrieve SFP (Z2) Idx."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(365));
		return (PH_FAILED);	
	}

	retSt = GetDynCurrentMast(&dynMast);
	if(retSt != RT_NORMAL){
		_stprintf_s(PPH_LogMsg, _T("Error: unable to update dest. cell situation."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(365));
		return (PH_FAILED);	
	}

	retSt = GetMastSituation(&mastSituation);
	if(retSt != RT_NORMAL){
		_stprintf_s(PPH_LogMsg, _T("Error: unable to retrieve mast situation."));
		PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(63));
		return (PH_FAILED);	
	}
	
	if((newMast == FALC_MAST) || (newMast == DMP_MAST)){
		//Gestione del dispositivo per il controllo del livello di installazione dei FA.//
		if((currMast == FALC_MAST) || (newMast == DMP_MAST))
			return(PH_NORMAL);
		retSt = GetDynMmxyPos(&dynx, &dyny);
		if(retSt != RT_NORMAL){
			_stprintf_s(PPH_LogMsg, _T("Error: unable to retrieve dynamic mast position."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(63));
			return (PH_FAILED);	
		}
		/*
		phstat = Mov_xy(0, 0, FALC_HOME_VX, FALC_HOME_VY, dynx, dyny, 0, 0, -1);
		if(phstat != PH_NORMAL){
			_stprintf_s(PPH_LogMsg, _T("Error: unable to take FALC MAST to home position."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(63));
			return (phstat);	
		}
		*/
		//Mi sposto dell'offset richiesto//
		retSt = GetZoneIdx(ZONE_REACTOR, &zoneIdx);
		phstat = Mov_xy_Exec(zoneIdx, (float)(dynx) , (float)(FALC_HOME_PX - falv_x), (float)(dyny), (float)(FALC_HOME_PY - falv_y));
		if(phstat != PH_NORMAL){
			_stprintf_s(PPH_LogMsg, _T("Error: unable to take FALC to its home position."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(63));
			return (phstat);	
		}
		retSt = SetDynCurrentMast(FALC_MAST);
		if(retSt != RT_NORMAL){
			_stprintf_s(PPH_LogMsg, _T("Error: unable to change mast to FALC."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(63));
			return (phstat);	
		}
		retSt = SetDynFCxyPos((float)(FALC_HOME_PX), (float)(FALC_HOME_PY));
		if(retSt != RT_NORMAL){
			_stprintf_s(PPH_LogMsg, _T("Error: unable to retrieve dynamic mast position."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(63));
			return (PH_FAILED);	
		}
		retCnd = PathStepAdd(FALC_MAST, (float)(FALC_HOME_PX),(float)(FALC_HOME_PY), PATH_TYPE_PLAN);
		if(retCnd != R_NORMAL){
			_stprintf_s(PPH_LogMsg, _T("Error: unable to add step to FC mast path."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(63));
			return (phstat);	
		}
	}
	else if((currMast == FALC_MAST) || (currMast == DMP_MAST)){
		//Gestione del dispositivo per il controllo del livello di installazione dei FA.//
		if((newMast == FALC_MAST) || (currMast == DMP_MAST))
			return(PH_NORMAL);
		retSt = GetDynFCxyPos(&dynx, &dyny);
		if(retSt != RT_NORMAL){
			_stprintf_s(PPH_LogMsg, _T("Error: unable to retrieve dynamic mast position."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(63));
			return (PH_FAILED);	
		}
		//if (!(((dynx >= (float)(FALC_HOME_VX - TOLL_MM))) && (dynx <= (float)(FALC_HOME_VX + TOLL_MM))) && ((dyny >= (float)(FALC_HOME_VY - TOLL_MM)) && (dyny <= (float)(FALC_HOME_VY + TOLL_MM)))){
			phstat = Mov_xy(0, 0, FALC_HOME_VX, FALC_HOME_VY, dynx, dyny, 0, 0, -1);
			if(phstat != PH_NORMAL){
				_stprintf_s(PPH_LogMsg, _T("Error: unable to take FC to home position."));
				PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(63));
				return (phstat);	
			}
		//}
		retSt = SetDynCurrentMast(MAIN_MAST);
		if(phstat != PH_NORMAL){
			_stprintf_s(PPH_LogMsg, _T("Error: unable to change mast to FALC."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(63));
			return (phstat);	
		}
		//Mi sposto dell'offset richiesto//
		/*
		retSt = GetZoneIdx(ZONE_REACTOR, &zoneIdx);
		phstat = Mov_xy_Exec(zoneIdx, (float)(FALC_HOME_PX - falv_x) , (float)(FALC_HOME_PX), (float)(FALC_HOME_PY - falv_y), (float)(FALC_HOME_PY));
		if(phstat != PH_NORMAL){
			_stprintf_s(PPH_LogMsg, _T("Error: unable to take FALC to its home position."));
			PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(63));
			return (phstat);	
		}
		*/
		if(newMast != MAIN_MAST){
			phstat = ChangeMast(MAIN_MAST, newMast);
			if(phstat != PH_NORMAL){
				_stprintf_s(PPH_LogMsg, _T("Error: cannot change to MAIN MAST."));
				PPH_LogFileWrite(_T("ERR"), PPH_LogMsg, PPH_ERR_CODE(354));
				return (phstat);
			}
		}
		else{
			retSt = GetMastPosEx(MAIN_MAST, &dynX, &dynY);
			PathStepAdd(MAIN_MAST, dynX, dynY, PATH_TYPE_PLAN);
		}
	}
	return(PH_NORMAL);
}
